/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.presenter;

import org.zhiqim.account.ZacSessionUser;
import org.zhiqim.account.api.pay.AlipayServer;
import org.zhiqim.account.dbo.user.UserCertification;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Updater;

import com.alipay.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;

/**
 * 
 * 用户实名认证处理器
 * 
 * @version v1.0.0 @author zhoulingbo 2018-7-5 新建与整理
 */
@AnAlias("UserCertifyPresenter")
public class UserCertifyPresenter
{

    /** 认证初始化 */
    public static void doInitCertification(HttpRequest request) throws Exception
    {
        String certName = request.getParameter("certName");
        String certNo = request.getParameter("certNo");
        String transactionId = "ZM" + Ids.longId();

        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        long userId = sessionUser.getUserId();

        ZhimaCustomerCertificationInitializeResponse resp = Global.getService(AlipayServer.class).certificationInit(transactionId, certName, certNo);
        if (resp.isSuccess())
        {
            String bizNo = resp.getBizNo();

            UserCertification cert = new UserCertification();
            cert.setUserId(userId);
            cert.setBizNo(bizNo);
            cert.setCertificationStatus(0);
            cert.setCertName(certName);
            cert.setCertNo(certNo);
            cert.setTransactionId(transactionId);
            cert.setCertificationEndTime(DateTimes.getNextDateTimeStringBySecond(10 * 60));

            Global.get(ZTable.class).insert(cert);
        }
        request.setResponseResult(transactionId);
    }

    /** 查询认证结果 */
    public static void doQueryCertification(HttpRequest request) throws Exception
    {
        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        long userId = sessionUser.getUserId();
        String tid = request.getParameter("tid");

        UserCertification item = Global.get(ZTable.class).item(UserCertification.class, tid);
        if (item == null)
        {
            request.setResponseError("未找到认证信息,请重新开始认证!");
            return;
        }

        if (item.getCertificationStatus() == 2 || item.getCertificationStatus() == 3)
        {
            request.setResponseError("认证失败或认证已失效,请重新开始认证!");
            return;
        }

        if (item.getCertificationStatus() == 1)
        {
            request.setResponseResult("认证成功!");
            return;
        }

        String bizNo = item.getBizNo();
        ZhimaCustomerCertificationQueryResponse resp = Global.getService(AlipayServer.class).certificationQuery(bizNo);
        if (resp.isSuccess() && "true".equals(resp.getPassed()))
        {
            Updater updater = new Updater();
            updater.addMust("transactionId", tid);
            updater.addField("certificationStatus", 1);
            Global.get(ZTable.class).update(UserCertification.class, updater);

            Updater updater2 = new Updater();
            updater2.addMust("userId", userId);
            updater2.addField("certStatus", 1);
            updater2.addField("certName", item.getCertName());
            updater2.addField("certNo", item.getCertNo());
            Global.get(ZTable.class).update(UserInfo.class, updater2);

            request.setResponseResult("认证成功!");
            return;
        }
        else
        {
            Updater updater = new Updater();
            updater.addMust("transactionId", tid);
            updater.addField("certificationStatus", 2);
            Global.get(ZTable.class).update(UserCertification.class, updater);

            request.setResponseError(resp.getMsg());
            return;
        }
    }
}
